package rd.hardcore.web.ui 
{
	import aze.motion.eaze;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.utils.ByteArray;
	import rd.hardcore.web.controls.PlayPause;
	
	/**
	 * ...
	 * @author Romain
	 */
	public class PicView extends Sprite
	{
		
		private var loader 	: Loader;
		private var bmp		: Bitmap; 
		private var size	: int = 80;
		
		public function PicView() 
		{
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleFail);
		}
		
		
		
		public function display( _data : ByteArray ) : void
		{
			loader.loadBytes( _data );
		}
		
		private function handleFail(e:IOErrorEvent):void 
		{
			trace("fail");
			clearListeners();
		}
		
		private function handleComplete(e:Event):void 
		{
			clearListeners();
			
			bmp = loader.content as Bitmap;
			bmp.alpha = 0;
			bmp.smoothing = true;
			
			var _w : int = bmp.width;
			var _h : int = bmp.height;
			
			var ratioImg : Number = _w / _h;
			
			if (ratioImg < 1) {
				
				bmp.width = size;
				bmp.height = _h / _w * size;
				
			}else {
				
				bmp.height = size;
				bmp.width = _w / _h * size;
			}
						
			bmp.x = (	size - bmp.width	) 	>> 1;
			bmp.y = (	size - bmp.height	) 	>> 1;
						
			addChild(bmp);
			
			eaze(bmp).to( .5, { alpha:1 } ); 
			
		}
		
		private function clearListeners () : void
		{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleComplete);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleFail);
		}
		
		public function handleOver( evt : MouseEvent ) : void
		{
			if (!bmp) return;
						
			eaze(bmp).to( .35, { alpha:.5 } );
		}
		
		public function handleOut( evt : MouseEvent ) : void
		{
			if (!bmp || PlayPause.isplaying) return;
						
			eaze(bmp).to( .3, { alpha:1 } );
		}


		
	}

}